package homeWorkCode;

// 多项式求和


import node.PolynomialNode;

public class Polynomial2<T> {

    PolynomialNode<T> head = new PolynomialNode<>();
    PolynomialNode<T> tail = this.head;

    Polynomial2() {
    }

    void append(PolynomialNode<T> data) {
        if (null != data) {
            this.tail.setNext(data);
            this.tail = tail.getNext();
        }
    }

    void sum(PolynomialNode<T> node) {
        PolynomialNode<T> tmp = node;
        PolynomialNode<T> opt = null;
        boolean success = false;
        while (null != tmp.getNext()) {
            opt = this.head;
            while (null != opt.getNext()) {
                if (tmp.getX() == opt.getX()) {
                    opt.setA(opt.getA() + tmp.getA());
                    opt.setA(opt.getB() + tmp.getB());
                    success = true;
                    break;
                }
                opt = opt.getNext();
            }
            if (!success) {
                PolynomialNode<T> new_node = new PolynomialNode<>(opt.getA(), opt.getX(), opt.getB());
                this.tail.setNext(new_node);
                this.tail = this.tail.getNext();
            }
            tmp = tmp.getNext();
        }


    }

    void show() {
        PolynomialNode<T> tmp = this.head.getNext();

        while (null != tmp.getNext()) {
            System.out.print(tmp.toString() + " ");
            tmp = tmp.getNext();
        }

    }

    public static void main(String[] args) {
        Polynomial2<Integer> polynomial = new Polynomial2<Integer>();

        for (int i = 1; i < 11; i++) {
            polynomial.append(new PolynomialNode<Integer>(i, i + 1, i + 2));
        }


        polynomial.show();

    }
}